<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
      <el-form-item label="车牌号" prop="deviceNumber">
        <el-input
          v-model="queryParams.deviceNumber"
          placeholder="请输入车牌号"
          clearable
          style="width: 200px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="报警类型" clearable style="width: 200px">
          <el-option v-for="dict in typeList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="postList">
      <el-table-column label="车牌号" align="center" prop="carNumber">
        <template #default="scope">
          {{ scope.row.car && scope.row.car.carNumber }}
        </template>
      </el-table-column>
      <el-table-column label="报警类型" align="center" prop="type">
        <template #default="scope">
          {{ typeList.find(i => i.value === scope.row.type)?.label }}
        </template>
      </el-table-column>
      <el-table-column label="报警内容" align="center" prop="content" />
      <el-table-column label="报警时间" align="center" prop="alarmTime" />
      <el-table-column label="是否已读" align="center" prop="type">
        <template #default="scope">
          <span v-if="scope.row.beRead === 1" style="color: #228b22">已读</span>
          <span v-else style="color: brown">未读</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Edit"
            @click="handleLook(scope.row, scope.$index)"
            v-hasPermi="['system:post:edit']"
            >查看图片</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog title="报警图片" v-model="open" width="500px" append-to-body>
      <el-carousel :interval="5000" arrow="always">
        <el-carousel-item v-for="item in imgList" :key="item">
          <img :src="item" width="460" alt="" />
        </el-carousel-item>
      </el-carousel>
    </el-dialog>
  </div>
</template>

<script setup name="Post">
import { getAlarmList, alarmRead } from '@/api/business/alarm';
import { ElMessage } from 'element-plus';

const { proxy } = getCurrentInstance();

const postList = ref([]);
const loading = ref(true);
const showSearch = ref(true);
const open = ref(false);
const imgList = ref([]);
const total = ref(0);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceNumber: null,
    sex: null,
  },
});
const typeList = [
  {
    value: 1,
    label: '疲劳驾驶',
  },
  {
    value: 2,
    label: '吸烟',
  },
  {
    value: 3,
    label: '打电话',
  },
];

const { queryParams, form, rules } = toRefs(data);

/** 查询岗位列表 */
function getList() {
  loading.value = true;
  getAlarmList(queryParams.value).then(response => {
    postList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm('queryRef');
  handleQuery();
}
function handleLook(row, index) {
  // console.log()
  if (row.beRead === 0) {
    alarmRead({ id: row.id, beRead: 1 });
    postList.value[index].beRead = 1;
  }
  if (!row.imageList?.length) {
    ElMessage.error('暂无图片');
    return;
  }
  open.value = true;
  imgList.value = row.imageList;
}

getList();
</script>
